//
//  ThreadDependenceViewController.swift
//  SwiftDemo
//
//  Created by Jason on 15/6/1.
//  Copyright (c) 2015年 Jason Jiang. All rights reserved.
//

import UIKit

class ThreadDependenceViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        var queue = NSOperationQueue();
        queue.maxConcurrentOperationCount = 5
        var op = blockOperation ()
        op.imgUrl = "http://g.hiphotos.baidu.com/image/pic/item/359b033b5bb5c9ea7337d84cd639b6003bf3b35d.jpg";
        op.block = {
            
            (img :UIImage) -> Void in
            
            
            var imgV = UIImageView (image: img);
            imgV.frame = CGRectMake(0, 64, 375, 400)
            self.view.addSubview(imgV)
            
        }
        
        op.bk = ({
            
            (a:Int,b:Int) -> (String)->String in
            
            func sumrsult(res:String) -> String
            {
                let c = a*a+b*a
                return "\(res) \(a)*\(a)+\(b)*\(a) = \(c)"
            }
            return sumrsult
            
            }
    )
    
        
        var opp = blockOperation ()
        opp.imgUrl = "http://g.hiphotos.baidu.com/image/pic/item/359b033b5bb5c9ea7337d84cd639b6003bf3b35d.jpg";
        opp.block = {
            
            (img :UIImage) -> Void in
            
            
            var imgV = UIImageView (image: img);
            imgV.frame = CGRectMake(0, 64, 375, 400)
            self.view.addSubview(imgV)
            
        }
        
        opp.bk = ({
            
            (a:Int,b:Int) -> (String)->String in
            
            func sumrsult(res:String) -> String
            {
                let c = a*a+b*a
                return "opp\(res) \(a)*\(a)+\(b)*\(a) = \(c)"
            }
            return sumrsult
            
            }
        )
        
        op.addDependency(opp)
        
        
        queue.addOperation(op)
        queue.addOperation(opp)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
